<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $table = 'goods';

    protected $fillable = [
        'user_id', 'debt','title','publicity_day','auction','auction_day', 'type_id', 'publicity', 'price', 'scope_price', 'start_price', 'reserve_price', 'cycle_price', 'delay', 'rules_price', 'pay_type', 'status', 'auction_status'
    ];


    /**
     * 添加产品
     * @param $data
     * @param array $images
     * @param array $files
     * @return bool|string
     */
    static public function  addGoods($data, $images = [], $files = [])
    {
        try {
            $re = self::create($data);
            $re->detail()->create(['introduce'=>$data['introduce']]);
            if (count($images) > 0) {
                $re->image()->createMany($images);
            }
            if (count($files) > 0) {
                $re->file()->createMany($files);
            }
            return true;
        } catch (\Exception $exception) {
            return false;
//            self::setErr($exception->getMessage());
//            return $exception->getMessage();
        }

    }

    /**
     * 图片介绍
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function image()
    {
        return $this->hasMany(GoodsFile::class, 'goods_id', 'id')->where('type', 1);
    }

    /**
     * 文件介绍
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function file()
    {
        return $this->hasMany(GoodsFile::class, 'goods_id', 'id')->where('type', 2);
    }

    /**
     * 推荐产品
     * @return mixed
     */
    static  public  function recommend()
    {
        $list = self::where('is_recommend', 1)->get();
        return $list;
    }

    /**
     * 拍卖纪录
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function auctions()
    {
        return $this->hasMany(AuctionLog::class, 'goods_id', 'id')
            ->limit(5)
            ->select('auction_logs.id', 'auction_logs.goods_id', 'auction_logs.user_id', 'auction_logs.price', 'auction_logs.created_at', 'users.phone')
            ->join('users', 'users.id', '=', 'auction_logs.user_id')
            ->orderBy('id','desc');
    }


    /**
     * @param $userID  用户
     * @param $status  审核状态
     * @param $auctionStatus  竞拍状态
     * @return mixed
     */
    static  public  function getMyReleaseGoodsAll($userID, $status)
    {
        $data = self::where('user_id', $userID)->Status($status)->with('detail','file','image')->orderBy('id','desc')->paginate();
        return $data;
    }


    /**
     *我的竞拍
     * @param $goods_ids
     * @param $auctionStatus
     * @return mixed
     */
    static public function getMyAuctionGoodsAll($goods_ids = [], $auctionStatus)
    {
        $data = self::whereIn('id', $goods_ids)->where('status', 2)->AuctionStatus($auctionStatus)->orderBy('id','desc')->paginate();

        return $data;
    }

    /**
     * 发布项目审核状态搜索
     * @param $query
     * @param $status
     */
    public function scopeStatus($query, $status)
    {
        if ($status) {
            $query->where('status', $status);
        }

    }

    /**
     * 审核通过后的状态
     * @param $query
     * @param $auctionStatus
     */
    public function scopeAuctionStatus($query, $auctionStatus)
    {
        if ($auctionStatus) {
            $query->where('auction_status', $auctionStatus);
        }

    }

    /**
     * 产品标题搜索
     * @param $query
     * @param $title
     */
    public function  scopeTitle($query,$title)
    {
        if($title){
            $query->where('goods.title','like','%'.$title.'%');
        }

    }

    /**
     * 昵称搜索
     * @param $query
     * @param $name
     */
    public function  scopeUserName($query,$name)
    {
        if($name){
            $query->where('users.name','like','%'.$name.'%');
        }
    }

    /**
     * 账号搜索
     * @param $query
     * @param $phone
     */
    public function  scopePhone($query,$phone)
    {
        if($phone){
            $query->where('users.phone','like','%'.$phone.'%');
        }

    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return bool
     */
    static  public function  saveStatus($id,$status)
    {
        $re = self::find($id);
        $re->status = $status;
        if($re->save()){
            return true;
        }

        return false;
    }


    /**
     * 编辑
     * @param $id
     * @param $data
     * @param array $files
     * @param array $images
     * @return bool
     */
    static  public function   edit($id,$data,$files=[],$images=[])
    {
        try {
            \DB::beginTransaction();
            $goods = self::find($id);
            $goods->title = $data['title'];
            $goods->price = $data['price'];
            $goods->start_price = $data['start_price'];
            $goods->start_price = $data['start_price'];
            $goods->scope_price = $data['scope_price'];
            $goods->reserve_price = $data['reserve_price'];
            $goods->priorityer = $data['priorityer'];
            $goods->pay_type = $data['pay_type'];
            $goods->status = 1;
            $goods->save();

            if (!$goods->save()) {
               throw  new  \Exception('失败');
            }
            //修改介绍
            GoodsDetail::where('goods_id',$id)->update([
                'introduce' => $data['introduce']
            ]);
            //文件图片
            if (count($images) > 0) {
                $goods->image()->delete();
                $goods->image()->createMany($images);
            }
            if (count($files) > 0) {
                $goods->file()->delete();
                $goods->file()->createMany($files);
            }
            \DB::commit();
            return true;
        }catch (\Exception $exception){
            info($exception->getMessage());
            return false;
        }



    }

    /**
     *后台编辑产品
     * @param $id
     * @param $data
     * @return bool
     */
  static  public function adminEdit($id,$data)
    {
        $goods = self::find($id);
        $goods->title = $data['title'];
        $goods->type_id = $data['type_id'];
        $goods->publicity = $data['publicity'];
        $goods->publicity_day = $data['publicity_day'];

        $goods->auction = $data['auction'];
        $goods->auction_day = $data['auction_day'];
        $goods->debt = $data['debt'];
//        $goods->start_price = $data['start_price'];
//        $goods->scope_price = $data['scope_price'];

//        $goods->reserve_price = $data['reserve_price'];
//        $goods->cycle_price = $data['cycle_price'];
        $goods->delay_time = $data['delay_time'];
        $goods->debt_date = $data['debt_date'];
        $goods->status = $data['status'];
        $goods->message = $data['message'];
        $goods->debt = $data['debt'];
//      $goods->introduce = $data['introduce'];
//      $goods->notice = $data['notice'];
//      $goods->instructions = $data['instructions'];
       $goods->save();

        //修改介绍
        GoodsDetail::where('goods_id',$id)->update([
            'notice'=>$data['notice'],
            'instructions'=>$data['instructions'],
            'introduce'=>$data['introduce'],
        ]);



      return true;


    }


    /**
     * 管关联详情
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function  detail()
    {

        return $this->hasOne(GoodsDetail::class,'goods_id','id');
    }
}
